
# Sanghyun Hong, February 23, 2019

rm(list = ls())
###################################
## Simulation Environment Setup  ##
## Simulation Environment Setup  ##
#################################################################
pathname <<- "C:/Temp/SimulationProject/";
setwd(pathname)
simN <<- 3000;
#################################################################
#
#
#
#
#
#################################################################
# Import Packages and set seed
#################################################################
library(metafor)
library(effsize)
library(sqldf) # This pakcage is for SQL
library(gridExtra)
library(grid)
library(lmtest)
library(sandwich)
library(clubSandwich)
library(splines)
library(stats)
library(ggplot2)
library(gridExtra)
library(cowplot)
library(matrixStats)
set.seed(3);
#################################################################
#
#
#
#
#
#################################################################
# Running Simulation
#################################################################
source(paste(pathname,"/SimCodes/SimulationEnvironment01.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment02.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment03.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment04.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment05.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment06.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment07.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment08.R", sep=""))
source(paste(pathname,"/SimCodes/SimulationEnvironment09.R", sep=""))
#################################################################
#
#
#
#
#
#################################################################
# Making Table 3 (MSE)
#################################################################
BiasListCsv <- c("01_BIAS.csv","02_BIAS.csv","03_BIAS.csv","04.csv","05.csv","06.csv","07.csv","08_BIAS.csv","09_BIAS.csv")
MiscListCsv <- c("01_Miscellaneous.csv","02_Miscellaneous.csv","03_Miscellaneous.csv","04_Miscellaneous.csv","05_Miscellaneous.csv","06_Miscellaneous.csv","07_Miscellaneous.csv","08_Miscellaneous.csv","09_Miscellaneous.csv")
CovListCsv <- c("01_COV.csv","02_COV.csv","03_COV.csv","04.csv","05.csv","06.csv","07.csv","08_COV.csv","09_COV.csv")
MSEListCsv <- c("01_MSE.csv","02_MSE.csv","03_MSE.csv","04.csv","05.csv","06.csv","07.csv","08_MSE.csv","09_MSE.csv")

Table3<-as.data.frame(matrix(0, nrow=144, ncol=10))
colnames(Table3)<-c("SimulationEnvironment","EffectSize","I2","WLSFE","WLSRE","PP","WAAP","WAAP*","AK1","AK2")

bgn_row<-1;
end_row<-0;
for (i in 1:9){
  if(i<4 || i>7){
    TmpDT01 <- read.csv(paste(pathname,"/Output/SimulationEnvironment",MSEListCsv[i], sep=""))
    TmpDT02 <- read.csv(paste(pathname,"/Output/SimulationEnvironment",MiscListCsv[i], sep=""))
    end_row <- end_row + nrow(TmpDT01)
    Table3[bgn_row:end_row, ]<-cbind(i, TmpDT01[,2], TmpDT02[,18]/100, TmpDT01[,c(6,7,10,8,9,11,12)])
    bgn_row <- bgn_row + nrow(TmpDT01)
  }else{
    TmpDT01 <- read.csv(paste(pathname,"/Output/SimulationEnvironment",MSEListCsv[i], sep=""))
    end_row <- end_row + nrow(TmpDT01)
    Table3[bgn_row:end_row, ]<-cbind(i, TmpDT01[,c(4,26)], TmpDT01[,c(9,12,6,15,18,21,24)])
    bgn_row <- bgn_row + nrow(TmpDT01)   
  }
}
Table3[,c(4:10)]<-Table3[,c(4:10)]/(rowMins(as.matrix(Table3[,c(4:10)])))
View(Table3)
write.csv(Table3, paste(pathname,"/Output/Table3.csv", sep=""))
#################################################################



#################################################################
# End of Program
#################################################################